package com.gzsxy.NIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 通过fileChannel读文件
 * @date 2022/4/23 19:58
 */
public class NIOFileChannel02 {

    public static void main(String[] args) throws Exception {
        File file = new File("d:\\file01.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        //通过FileInputStream 获取对应的fileChannel
        FileChannel fileChannel = fileInputStream.getChannel();
        //创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
        //将通道的数据读入到Buffer
        fileChannel.read(byteBuffer);
        //将字节转成字符串
        System.out.println(new String(byteBuffer.array()));


    }
}
